package com.lc.qianrenshiye.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.BasePictureActivity;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.ClickUpdtaeUserInfoPost;
import com.lc.qianrenshiye.conn.UpdateHeadImg;
import com.lc.qianrenshiye.conn.UpdateLicencePost;
import com.lc.qianrenshiye.conn.UserInfoPost;
import com.lc.qianrenshiye.dialog.PictureDialog;
import com.lc.qianrenshiye.uitls.MyAnimationUitls;
import com.lc.qianrenshiye.uitls.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.entity.AppCrop;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;

import java.io.File;

/**
 * Created by Administrator on 2016/11/17.
 */
public class UserInfoActivity extends BasePictureActivity implements View.OnClickListener{

    @BoundView(R.id.user_info_ll) private LinearLayout user_info_ll;

    //店名
    @BoundView(R.id.user_info_shop_name_tv) private TextView user_info_shop_name_tv;
    @BoundView(R.id.user_info_change_shop_name_tv) private TextView user_info_change_shop_name_tv;

    //户主
    @BoundView(R.id.user_info_username_tv) private TextView user_info_username_tv;
    @BoundView(R.id.user_info_change_username_tv) private TextView user_info_change_username_tv;

    //联系方式
    @BoundView(R.id.user_info_call_tv) private TextView user_info_call_tv;
    @BoundView(R.id.user_info_call_change_tv) private TextView user_info_call_change_tv;

    //头像更换
    @BoundView(R.id.user_info_head_iv) private ImageView user_info_head_iv;
    @BoundView(R.id.user_info_update_head_tv) private TextView user_info_update_head_tv;

    //营业执照
    @BoundView(R.id.user_info_licence_iv) private ImageView user_info_licence_iv;
    @BoundView(R.id.user_info_update_licence_tv) private TextView user_info_update_licence_tv;

    //绑定银行卡
    @BoundView(R.id.user_info_bank_iv) private ImageView user_info_bank_iv;
    @BoundView(R.id.user_info_update_bank_tv) private TextView user_info_update_bank_tv;

    private String headUrl;

    private UserInfoPost userInfoPost = new UserInfoPost(new AsyCallBack<UserInfoPost.UserInfoEntity>() {

        @Override
        public void onSuccess(String toast, int type, UserInfoPost.UserInfoEntity userInfoEntity) throws Exception {

            user_info_shop_name_tv.setText(userInfoEntity.title);
            user_info_username_tv.setText(userInfoEntity.username);
            user_info_call_tv.setText(userInfoEntity.phone);

            GlideLoader.getInstance().get(context, userInfoEntity.picurl, user_info_head_iv, R.mipmap.holder_head, GlideLoader.TYPE_IMAGE_CIRCLE);
            GlideLoader.getInstance().get(context, userInfoEntity.licence, user_info_licence_iv, R.mipmap.holder_licence, GlideLoader.TYPE_IMAGE_FILLET);

            MyAnimationUitls.toVisibleAnim(user_info_ll);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    private ClickUpdtaeUserInfoPost clickUpdtaeUserInfoPost = new ClickUpdtaeUserInfoPost(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            MyToast.showToast(s, context);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });

    private UpdateLicencePost updateLicencePost = new UpdateLicencePost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            MyToast.showToast(s, context);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });

    private UpdateHeadImg updateHeadImg = new UpdateHeadImg(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            MyToast.showToast(s, context);

            if (!TextUtils.isEmpty(headUrl)) {

                try {

                    BusinessCenterActivity.RefreshTitleAndHead refreshTitleAndHead = (BusinessCenterActivity.RefreshTitleAndHead) getAppCallBack(BusinessCenterActivity.class);
                    refreshTitleAndHead.refreshHead(headUrl);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    private int photoType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        setBackTrue();

        setTitleName(getIntent().getExtras().getString("shopName", ""));

        userInfoPost.sid = BaseApplication.BasePreferences.readShopId();
        userInfoPost.execute(this);

        user_info_change_shop_name_tv.setOnClickListener(this);
        user_info_change_username_tv.setOnClickListener(this);
        user_info_call_change_tv.setOnClickListener(this);
        user_info_update_head_tv.setOnClickListener(this);
        user_info_update_licence_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.user_info_change_shop_name_tv:

                clickUpdtaeUserInfoPost.sid = BaseApplication.BasePreferences.readShopId();
                clickUpdtaeUserInfoPost.type = "title";
                clickUpdtaeUserInfoPost.execute(this);

                break;
            case R.id.user_info_change_username_tv:

                clickUpdtaeUserInfoPost.sid = BaseApplication.BasePreferences.readShopId();
                clickUpdtaeUserInfoPost.type = "name";
                clickUpdtaeUserInfoPost.execute(this);

                break;
            case R.id.user_info_call_change_tv:

                clickUpdtaeUserInfoPost.sid = BaseApplication.BasePreferences.readShopId();
                clickUpdtaeUserInfoPost.type = "phone";
                clickUpdtaeUserInfoPost.execute(this);

                break;
            case R.id.user_info_licence_iv:

                break;
            case R.id.user_info_update_head_tv:

                photoType = 1;

                new PictureDialog(context) {
                    @Override
                    protected void onCamera() {

                        startCamera(null);

                    }

                    @Override
                    protected void onAlbum() {

                        startAlbum(null);

                    }
                }.show();


                break;

            case R.id.user_info_update_licence_tv:

                photoType = 2;

                new PictureDialog(context) {
                    @Override
                    protected void onCamera() {

                        startCamera(null);

                    }

                    @Override
                    protected void onAlbum() {

                        startAlbum(null);

                    }
                }.show();


                break;

            case R.id.user_info_update_bank_tv:

//                photoType = 3;
//
//                new PictureDialog(context) {
//                    @Override
//                    protected void onCamera() {
//
//                        startCamera(null);
//
//                    }
//
//                    @Override
//                    protected void onAlbum() {
//
//                        startAlbum(null);
//
//                    }
//                }.show();


                break;


        }

    }

    @Override
    protected AppCrop getCrop() {

        return new AppCrop().setCrop(true);

    }

    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {

        switch (photoType) {

            case 1:

                headUrl = s;

                updateHeadImg.sid = BaseApplication.BasePreferences.readShopId();
                updateHeadImg.picurl = new File(s);
                updateHeadImg.execute(UserInfoActivity.this);

                GlideLoader.getInstance().get(UserInfoActivity.this, s, user_info_head_iv, R.mipmap.holder_head, GlideLoader.TYPE_IMAGE_CIRCLE);

                break;
            case 2:

                updateLicencePost.sid = BaseApplication.BasePreferences.readShopId();
                updateLicencePost.licence = new File(s);
                updateLicencePost.execute(UserInfoActivity.this);

                GlideLoader.getInstance().get(UserInfoActivity.this, s, user_info_licence_iv, R.mipmap.holder_licence, GlideLoader.TYPE_IMAGE_FILLET);

                break;

        }

    }
}
